<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>11-显示隐藏文本框内容</title>
  <style>
    input {
      display: block;
      width: 200px;
      height: 30px;
      margin: 100px auto;
      border: 0;
      border-bottom: 1px solid #ccc;
      outline: none;
      color: #999;
    }
  </style>
</head>

<body>
  <input type="text" value="手机" name="" id="">

  <script>
    // 1. 获取元素
    var text = document.querySelector('input');
    // 2. 注册事件 获得焦点事件 onfocus
    text.onfocus = function () {
      if (this.value === '手机') {
        this.value = '';
      }
      // 获得焦点时需要将文本框里面的文字颜色变黑
      this.style.color = '#333';
    }

    text.onblur = function () {
      if (this.value === '') {
        this.value = '手机';
      }
      // 失去焦点时文字颜色变浅即可
      this.style.color = '#999';
    }
  </script>
</body>

</html>